<?php
/**
 * CakeCMS - 菜单管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class MenuController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', '菜单管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'menu.js' 
		) );
	}
	public function index() {
		$this->set ( 'title_for_layout', '菜单管理' );
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		$tree = new Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$this->loadModel ( "Menu" );
		
		$conditions = array ();
		$result = $this->Menu->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'listorder ASC',
						'id ASC' 
				) 
		) );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['Menu'] ['id'];
			$r ['name'] = $val ['Menu'] ['name'];
			$r ['parentid'] = $val ['Menu'] ['parentid'];
			$r ['controller'] = $val ['Menu'] ['controller'];
			$r ['data'] = $val ['Menu'] ['data'];
			$r ['listorder'] = $val ['Menu'] ['listorder'];
			$r ['display'] = $val ['Menu'] ['display'];
			$r ['str_manage'] = '<a href="/Menu/add/' . $val ['Menu'] ['id'] . '/">添加子菜单</a> | ';
			$r ['str_manage'] .= '<a href="/Menu/edit/' . $val ['Menu'] ['id'] . '/">修改</a> | ';
			$r ['str_manage'] .= '<a href="javascript:;" onclick="delete_menu(' . $val ['Menu'] ['id'] . ')">删除</a>';
			$array [] = $r;
		}
		
		$str = "<tr>
		<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
		<td align='center'>\$id</td>
		<td >\$spacer\$name</td>
		<td align='center'>\$str_manage</td>
		</tr>";
		
		$tree->init ( $array );
		$select_categorys = $tree->get_tree ( 0, $str );
		$this->set ( 'categorys', $select_categorys );
	}
	
	/**
	 * 添加菜单
	 */
	public function add() {
		if ($this->request->isPost ()) {
			$data ['Menu'] ['name'] = $this->data ['name'];
			if (empty ( $data ['Menu'] ['name'] )) {
				$this->flash ( '菜单名称不能为空!', '/Menu/add/', 1250 );
				return false;
			}
			$data ['Menu'] ['parentid'] = $this->data ['parentid'];
			if (empty ( $data ['Menu'] ['parentid'] )) {
				$this->flash ( '上级菜单不能为空!', '/Menu/add/', 1250 );
				return false;
			}
			$data ['Menu'] ['controller'] = $this->data ['controller'];
			if (empty ( $data ['Menu'] ['controller'] )) {
				$this->flash ( '功能地址不能为空!', '/Menu/add/', 1250 );
				return false;
			}
			$data ['Menu'] ['data'] = $this->data ['params'];
			$data ['Menu'] ['listorder'] = 0;
			$data ['Menu'] ['display'] = $this->data ['display'];
			
			$this->loadModel ( 'Menu' );
			if ($this->Menu->add ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( '添加菜单' . $data ['Menu'] ['name'] );
				
				$this->flash ( '添加菜单成功!', '/Menu/', 1250 );
				return false;
			} else {
				$this->flash ( '添加菜单失败!', '/Menu/add/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '添加菜单' );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "Menu" );
			
			$parentid = $this->params ['parentid'];
			
			$conditions = array ();
			$result = $this->Menu->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'listorder ASC',
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['Menu'] ['id'];
				$r ['name'] = $val ['Menu'] ['name'];
				$r ['parentid'] = $val ['Menu'] ['parentid'];
				$r ['controller'] = $val ['Menu'] ['controller'];
				$r ['data'] = $val ['Menu'] ['data'];
				$r ['listorder'] = $val ['Menu'] ['listorder'];
				$r ['display'] = $val ['Menu'] ['display'];
				$r ['selected'] = $val ['Menu'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'select_categorys', $select_categorys );
		}
	}
	
	/**
	 * 编辑菜单
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			$data ['Menu'] ['name'] = $this->data ['name'];
			if (empty ( $data ['Menu'] ['name'] )) {
				$this->flash ( '菜单名称不能为空!', '/Menu/add/', 1250 );
				return false;
			}
			$data ['Menu'] ['parentid'] = $this->data ['parentid'];
			if (empty ( $data ['Menu'] ['parentid'] )) {
				$this->flash ( '上级菜单不能为空!', '/Menu/add/', 1250 );
				return false;
			}
			$data ['Menu'] ['controller'] = $this->data ['controller'];
			if (empty ( $data ['Menu'] ['controller'] )) {
				$this->flash ( '功能地址不能为空!', '/Menu/add/', 1250 );
				return false;
			}
			$data ['Menu'] ['data'] = $this->data ['params'];
			$data ['Menu'] ['listorder'] = 0;
			$data ['Menu'] ['display'] = $this->data ['display'];
			
			$data ['Menu'] ['id'] = $this->data ['id'];
			
			$this->loadModel ( 'Menu' );
			if ($this->Menu->edit ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( '编辑菜单' . $data ['Menu'] ['name'] );
				
				$this->flash ( '编辑菜单成功!', '/Menu/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑菜单失败!', "/Menu/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '编辑菜单' );
			
			$id = $this->params ['id'];
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "Menu" );
			
			$conditions = array ();
			$result = $this->Menu->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'listorder ASC',
							'id ASC' 
					) 
			) );
			
			$parentid_Info = $this->Menu->GetMenuByID ( $id );
			$parentid = $parentid_Info ['Menu'] ['parentid'];
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['Menu'] ['id'];
				$r ['name'] = $val ['Menu'] ['name'];
				$r ['parentid'] = $val ['Menu'] ['parentid'];
				$r ['controller'] = $val ['Menu'] ['controller'];
				$r ['data'] = $val ['Menu'] ['data'];
				$r ['listorder'] = $val ['Menu'] ['listorder'];
				$r ['display'] = $val ['Menu'] ['display'];
				$r ['selected'] = $val ['Menu'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$Menu_info = $this->Menu->GetMenuByID ( $id );
			$this->set ( 'Menu_info', $Menu_info );
			$this->set ( 'select_categorys', $select_categorys );
			$this->set ( 'id', $id );
		}
	}
	
	/**
	 * 删除菜单
	 */
	public function delete() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数不能为空,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Menu" );
		if ($this->Menu->del ( $id )) {
			// 记录操作日志
			$this->AdminLog->addLog ( '删除菜单,菜单ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	
	/**
	 * 排序
	 */
	public function listorder() {
		if ($this->request->isPost ()) {
			$listorders = $this->data ['listorders'];
			if (is_array ( $listorders )) {
				$this->loadModel ( "Menu" );
				foreach ( $listorders as $id => $listorder ) {
					$data ['Menu'] ['id'] = $id;
					$data ['Menu'] ['listorder'] = $listorder;
					$this->Menu->edit ( $data );
				}
				
				// 记录操作日志
				$this->AdminLog->addLog ( '菜单排序' );
				
				$this->flash ( '排序操作成功!', "/Menu/", 1250 );
				return false;
			}
		} else {
			$this->flash ( '请正确提交菜单排序列表!', "/Menu/", 1250 );
			return false;
		}
	}
}
?>